package com.lulalei.dbviewer.commons.exception;

import com.lulalei.dbviewer.commons.api.ResultBean;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Objects;

@ResponseBody
@ControllerAdvice
public class CustomizeExceptionHandle {


    @ExceptionHandler(value =CustomizeException.class)
    public ResultBean<String> exceptionHandler(CustomizeException e){
        e.printStackTrace();
        return ResultBean.failure(e.getMessage());
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResultBean<String> notValidR(MethodArgumentNotValidException e) {
        return ResultBean.failure(Objects.requireNonNull(e.getBindingResult().getFieldError()).getDefaultMessage());
    }
}
